
<template>
  <div class="content">
    <link href="/static/css/work-team/addbat.css" rel="stylesheet" />
    <div
      class="content-right"
      id="addbat"
      v-cloak
      style="margin:0 auto;border:0;width:980px;background: #fff;text-align:left;"
    >
      <div class="con-tp">
        <span class="stp_1" :class="{'yes':step==1}">基础设置</span>
        <span class="stp_2" :class="{'yes':step==2}">高级设置</span>
      </div>
      <div class="con-list1" v-show="step==1">
        <div class="title">基础设置</div>
        <div class="stepitem">
          <div class="item_l with">
            <label>归属任务</label>
            <input type="text" name="taskinfoname" :value="info.public.taskName" readonly />
            <!-- 任务列表 -->
            <transition name="fade">
              <div class="givefile" v-if="taskLayer" style="z-index:9999;">
                <em @click="close">关闭</em>
                <div class="clearfix mn-con">
                  <div class="mn-con-rt">
                    <div class="label clearfix">
                      <input type="text" v-model="searchTaskName" placeholder="请输入任务名称" />
                      <a v-on:click="searchTask">确认</a>
                    </div>
                    <div class="bom-sel">
                      <ul>
                        <li
                          v-for="item in taskList"
                          :class="{'select':info.public.taskId==item.taskid}"
                          v-on:click="selectTask(item.taskid,item.name,item.task_team_id)"
                          :title="item.name"
                          :key="item.taskid"
                        >{{item.name}}</li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </transition>
          </div>
          <div class="item_r">
            <label>批次名称</label>
            <input
              type="text"
              ref="inputTask"
              v-on:focus="monitor($event)"
              name="batchname"
              v-model="info.public.batchName"
              placeholder="请输入批次名称"
              :disabled="info.public.edit"
            />
          </div>
        </div>
        <div class="stepitem">
          <label>标注规范</label>
          <textarea
            class="area"
            id="editor"
            name="batchrule"
            v-on:focus="monitor($event)"
            placeholder="请输入规范"
            v-model="info.public.rule"
            style="display: inline-block;width:760px;height: 120px;"
          ></textarea>
        </div>
        <div class="radioitem">
          <label>批次优先级</label>
          <ul class="radio">
            <li @click="levelChange(3)" :class="{'selected':info.public.level==3}">低</li>
            <li @click="levelChange(2)" :class="{'selected':info.public.level==2}">中</li>
            <li @click="levelChange(1)" :class="{'selected':info.public.level==1}">高</li>
            <li @click="levelChange(0)" :class="{'selected':info.public.level==0}">绝对优先</li>
          </ul>
        </div>
        <div class="radioitem">
          <label>批次状态</label>
          <ul class="radio">
            <li @click="executeChange(0)" :class="{'selected':info.public.paused==0}">立即执行</li>
            <li @click="executeChange(1)" :class="{'selected':info.public.paused==1}">暂不执行</li>
          </ul>
        </div>
        <div v-if="flag">
          <div id="baseSetting">
            <basesetting-component ref="base" :editBaseData="editBaseData"></basesetting-component>
          </div>
        </div>
        <div class="btngroup">
          <a v-on:click="nextstep">下一步</a>
        </div>
      </div>
      <!--con-list01-->
      <div class="con-list2" v-show="step==2">
        <div v-if="flag">
          <div id="specSetting">
            <specsetting-component ref="spec" :editSpecData="editSpecData"></specsetting-component>
          </div>
        </div>
        <div class="btngroup">
          <a v-on:click="nextstep">上一步</a>
          <a class="btnd" v-on:click="submitBtn(0)">提交</a>
          <a
            class="btnd"
            v-on:click="submitBtn(1)"
            v-if="statusType != 4 && statusType != 5 && statusType != 6"
            v-show="flowtask == null || flowtask == undefined"
          >保存</a>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import $ from "jquery";
import '../assets/ueditor/ueditor.config.js';
import '../assets/ueditor/ueditor.all.js';
import '../assets/ueditor/lang/zh-cn/zh-cn';
import '../assets/ueditor/ueditor.parse.js';
import BaseSetting from "@/components/BaseSetting";
import SpecSetting from "@/components/SpecSetting";
export default {
  name: "CreateBatch",
  data() {
    return {
      ue: null,
      editBaseData: "",
      editSpecData: "",
      repulse: "",
      jurisdiction: "",
      task_team_id: "",
      role: "",
      groupId: "",
      flowtask: "",
      batchId: "",
      edit: "",
      statusType: "",
      postData: {
        status: "4;5;6", //查询状态
        batchName: "", //批次名称
        taskName: "", //任务名称
        queryType: "page", //分页标识
        delFlag: ""
      },
      pageSize: 1,
      batchList: null,
      searchTaskName: "", //搜索任务列表
      taskLayer: false, //任务列表弹层
      taskList: [], //任务列表盒子
      step: 1, //基础、高级
      priority: 2, //记住当前批次的优先级
      flag: false,
      info: {
        public: {
          pickBatchId: "",
          pickTaskId: "",
          batchId: "", //
          edit: false,
          taskName: "", //归属任务名称
          taskId: "", //归属任务id
          batchName: "",
          rule: "",
          level: 2,
          paused: 0
        },
        custom: {
          base: {},
          spec: {}
        }
      }
    };
  },
  created(){
  },
  components: {
    "basesetting-component": BaseSetting,
    "specsetting-component": SpecSetting
  },
  methods: {
    nextstep: function() {
      var self = this;
      let htmlVal = self.ue.getContent(); //获取富文本编辑框的HTML
      let reg = /<[^<>]+>/g;
      let htmlValtext = htmlVal.replace(/<!--[\w\W\r\n]*?-->/gim, ""); //获取富文本编辑框的text
      htmlValtext = htmlValtext.replace(reg, "");
      htmlValtext = htmlValtext.replace(/&nbsp;/g, "").replace("/\n/g", "");
      self.info.public.rule = htmlVal;
      let reg2 = /^[A-Za-z0-9\-\u4e00-\u9fa5]+$/;
      if (self.info.public.taskName == "") {
        if (!$("input[name='taskinfoname']").hasClass("err")) {
          $("input[name='taskinfoname']")
            .removeClass("yes")
            .addClass("err")
            .after("<div class='error'>请先选择归属任务</div>");
        }
        return false;
      } else if (self.info.public.batchName == "") {
        if (!$("input[name='batchname']").hasClass("err")) {
          $("input[name='batchname']")
            .removeClass("yes")
            .addClass("err")
            .after("<div class='error'>请输入批次名称</div>");
        }
        return false;
      } else if (self.info.public.batchName.length > 40) {
        if (!$("input[name='batchname']").hasClass("err")) {
          $("input[name='batchname']")
            .removeClass("yes")
            .addClass("err")
            .after("<div class='error'>批次名称不能超过40个字符</div>");
        }
        return false;
      } else if (!reg2.test(self.info.public.batchName)) {
        if (!$("input[name='batchname']").hasClass("err")) {
          $("input[name='batchname']")
            .removeClass("yes")
            .addClass("err")
            .after("<div class='error'>批次名称支持中英文数字和-</div>");
        }
        return false;
      } else if (
        htmlValtext == 0 ||
        htmlValtext.length > 3000 ||
        htmlValtext.length < 1
      ) {
        if (!$("textarea[name='batchrule']").hasClass("err")) {
          $("textarea[name='batchrule']")
            .removeClass("yes")
            .addClass("err")
            .after("<div class='error'>标注规范需在1~3000字符</div>");
        }
        return false;
      } else {
        if (self.step == 1) {
          self.step = 2;
        } else {
          self.step = 1;
        }
      }
    },
    executeChange(n) {
      var self = this;
      if (self.info.public.taskId != "") {
        self.info.public.paused = n;
      } else {
        self.$message("请先选择归属任务");
      }
    },
    levelChange: function(n) {
      var self = this;
      if (self.info.public.taskId != "") {
        if (self.priority == n) {
          self.info.public.level = n;
        } else {
          var proxyurl =
            "/v1/bes/task/batch/judge_priority?taskId=" +
            self.info.public.taskId +
            "&priority=" +
            n +
            "&batchId=" +
            self.info.public.batchId;

          self.$api
            .method(proxyurl, "", "GET")
            .then(data => {
              if (data.rt.status == 200) {
                if (data.data) {
                  self.info.public.level = n;
                } else {
                  self.$message.error("此优先级批次已达上限");
                }
              } else {
                self.$message.error(data.data);
              }
            })
            .catch(err => {
              console.log(err);
            });
        }
      } else {
        self.$message("请先选择归属任务");
      }
    },
    taskLayerShow: function() {
      var self = this;
      if ($("input[name='taskinfoname']").hasClass("err")) {
        $("input[name='taskinfoname']")
          .removeClass("err")
          .addClass("yes");
        $("input[name='taskinfoname']")
          .next(".error")
          .remove();
      }
      this.getTaskList();
      self.taskLayer = true;
    },
    monitor: function(e) {
      let ev = e.currentTarget;
      if ($(ev).hasClass("err")) {
        $(ev)
          .removeClass("err")
          .addClass("yes");
      }
      $(ev)
        .next(".error")
        .remove();
    },
    getTaskList: function(type) {
      var self = this;
      self.flag = false;
      var proxyurl = "/v1/task/info?qtype=adminTask";
      if (type != "" && type != null && type != undefined) {
        proxyurl = "/v1/task/info?qtype=adminTask&name=" + type;
      }
      self.$api
        .method(proxyurl, "", "GET")
        .then(data => {
          if (data.rt.status == 200) {
            self.taskList = data.data;
          } else {
            self.$message(data.data);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    searchTask: function() {
      this.getTaskList(this.searchTaskName);
    },
    selectTask: function(id, name, teamid) {
      var self = this;
      self.info.public.taskId = id;
      self.info.public.taskName = name;
      self.taskLayer = false;
      if (!self.info.public.edit) {
        if (!teamid) {
          //流转任务不带入批次信息
          if (this.flowtask) {
            console.log("这里调用的是批次组件");
          } else {
            this.getTaskBatch(id, name, teamid);
          }
        } else {
          this.getTaskBatch(id, name, teamid);
        }
      }
      self.flag = true;
    },
    getTaskBatch(id, name, teamid) {
      // console.log(this.groupId)
      var self = this;
      var mydata = {};
      mydata.status = self.postData.status;
      mydata.batchName = self.postData.batchName;
      mydata.queryType = self.postData.queryType;
      mydata.dealType = "default";
      var proxyurl = "/v1/bes/task/batch?taskId=" + id;
      self.PageNav.load(1, self.pageSize, proxyurl, mydata, function(data) {
        self.batchList = data.list;
        if (JSON.stringify(data.list) != "[]") {
          self.getBatchInfo(self.batchList[0].batchId, teamid);
        } else {
          if (teamid) {
            // 为空不赋值
            self.groupId = teamid;
            self.info.public.batchName = "";
            self.info.public.level = 2;
            self.info.public.paused = 0;
            if (self.info.public.rule) {
              self.ue.setContent((self.info.public.rule = ""));
            }
          } else {
            self.groupId = self.GetQueryString("groupId");
            self.info.public.batchName = "";
            self.info.public.level = 2;
            self.info.public.paused = 0;
            if (self.info.public.rule) {
              self.ue.setContent((self.info.public.rule = ""));
            }
          }
        }
        self.role = teamid;
      });
    },
    getBatchInfo(id, teamid) {
      var self = this;
      if (id != "") {
        var proxyurl = "/v1/bes/task/batch?batchId=" + id;

        self.$api
          .method(proxyurl, "", "GET")
          .then(data => {
            if (data.rt.status == 200) {
              let _data = data.data[0];
              self.info.public.edit = false;
              // self.info.public.batchId = _data.batchId;
              self.info.public.taskName = _data.taskName;
              self.info.public.taskId = _data.taskId;
              self.info.public.batchName = _data.batchName;
              self.info.public.rule = _data.markStandard;
              self.info.public.level = _data.priority;
              if (_data.paused == null || _data.paused == undefined) {
                self.info.public.paused = 0;
              } else {
                self.info.public.paused = _data.paused;
              }
              self.priority = _data.priority;
              _data.extend.custom.base.myrid = "";
              if (_data.extend.custom.base.recycleTime) {
                delete _data.extend.custom.base.recycleTime;
              }
              self.$set(this, "editBaseData", _data.extend.custom.base);
              self.$set(this, "editSpecData", _data.extend.custom.spec);

              if (self.info.public.rule) {
                self.ue.setContent(self.info.public.rule);
              }
              if (teamid) {
                self.groupId = teamid;
                self.jurisdiction = teamid;
                self.info.public.batchName = "";
              } else {
                self.groupId = self.GetQueryString("groupId");
                self.jurisdiction = self.groupId;
                self.info.public.batchName = "";
              }
              self.flag = true;
            } else {
              self.$message(data.data);
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    },
    close: function() {
      var self = this;
      self.taskLayer = false;
      if (self.info.public.taskId != "") {
        self.selectTask(self.info.public.taskId, self.info.public.taskName);
      }
    },
    GetQueryString: function(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    submitBtn: function(n) {
      var self = this;
      //删除目录树
      this.$refs.base.fileList = [];
      if (this.$refs.spec.textareaStatus == 2) {
        this.$refs.spec.textareaStatus = 1;
      }
      self.info.custom.base = this.$refs.base;
      self.info.custom.spec = this.$refs.spec;
      if (
        (this.statusType == null || this.statusType == undefined) &&
        this.batchId == null
      ) {
        self.info.public.batchId = "";
      }
      self.info.public["type"] = n;
      if (self.myvalidate() == 0) {
        var proxyurl =
          "/v1/be/task/batch?taskId=" +
          self.info.public.taskId +
          "&groupId=" +
          this.groupId;

        self.$api
          .method(proxyurl, JSON.stringify(this.info), "POST")
          .then(data => {
            if (data.rt.status == 200) {
              if (n == 0) {
                self.$message("提交成功", "/manager/index/batch");
              } else {
                self.$message("保存成功", "/manager/index/batch");
              }
            } else {
              self.$message(data.data);
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    },
    //校验的规则
    myvalidate: function() {
      var i = 0;
      if (
        this.$refs.spec.score.mark.correctWeight.childWeight === "" &&
        this.$refs.spec.childLabel.isOpen
      ) {
        this.$message("子句属性权重不得为空");
        i += 1;
        return i;
      }

      this.$refs.spec.score.mark.correctWeight.fullWeight = parseFloat(
        this.$refs.spec.score.mark.correctWeight.fullWeight
      );
      this.$refs.spec.score.mark.correctWeight.childWeight = parseFloat(
        this.$refs.spec.score.mark.correctWeight.childWeight
      );
      this.$refs.spec.score.mark.correctWeight.slotWeight = parseFloat(
        this.$refs.spec.score.mark.correctWeight.slotWeight
      );
      this.$refs.spec.score.mark.correctWeight.wordWeight = parseFloat(
        this.$refs.spec.score.mark.correctWeight.wordWeight
      );
      this.$refs.spec.score.mark.correctWeight.textWeight = parseFloat(
        this.$refs.spec.score.mark.correctWeight.textWeight
      );

      var reg = /^[0-9]*[1-9][0-9]*$/; //正整数
      var regdec = /^([1-9]\d*|0)(\.\d{1,4})?$/; //3位小数验证

      if (
        this.$refs.base.layerstatus1 == false &&
        this.$refs.base.layerstatus2 == false &&
        this.$refs.base.layerstatus3 == false
      ) {
        this.$message("请选择基础设置中的文本层配置");
        i += 1;
        return i;
      } else if (
        this.$refs.base.checkStatus == true &&
        !reg.test(Number(this.$refs.base.checkVal))
      ) {
        this.$message("检查比例必须是小于等于100的正整数");
        i += 1;
        return i;
      } else if (
        this.$refs.base.checkStatus == true &&
        Number(this.$refs.base.checkVal) > 100
      ) {
        this.$message("检查比例必须是小于等于100的正整数");
        i += 1;
        return i;
      } else if (
        this.$refs.base.testStatus == true &&
        !reg.test(Number(this.$refs.base.testVal))
      ) {
        this.$message("质检比例必须是小于等于100的正整数");
        i += 1;
        return i;
      } else if (
        this.$refs.base.testStatus == true &&
        Number(this.$refs.base.testVal) > 100
      ) {
        this.$message("质检比例必须是小于等于100的正整数");
        i += 1;
        return i;
      } else if (
        this.exchangeStatus == true &&
        !reg.test(Number(this.$refs.spec.exchangeVal))
      ) {
        this.$message("积分换算比例必须小于等于5000的正整数");
        i += 1;
        return i;
      } else if (
        this.exchangeStatus == true &&
        Number(this.$refs.spec.exchangeVal) > 5000
      ) {
        this.$message("积分换算比例必须小于等于5000的正整数");
        i += 1;
        return i;
      } else if (this.$refs.base.importType == 3) {
        this.$message.error("请选择数据导入方式");
        i += 1;
        return i;
      } else if (
        this.$refs.base.importType == 0 &&
        this.$refs.base.myrid == ""
      ) {
        this.$message.error("请导入云盘数据");
        i += 1;
        return i;
      } else if (
        this.$refs.spec.score.mark.coe == "" &&
        this.$refs.spec.score.mark.coe != 0
      ) {
        this.$message.error("请输入标注积分设置");
        i += 1;
        return i;
      } else if (this.$refs.spec.score.mark.coe < 0) {
        this.$message.error("标注积分不能小于0");
        i += 1;
        return i;
      } else if (!regdec.test(this.$refs.spec.score.mark.coe)) {
        this.$message.error("标注积分为最多4位小数的数字");
        i += 1;
        return i;
      } else if (
        this.$refs.base.checkStatus &&
        this.$refs.spec.score.check.coe == "" &&
        this.$refs.spec.score.check.coe !== 0
      ) {
        this.$message.error("请输入检查积分设置");
        i += 1;
        return i;
      } else if (
        this.$refs.spec.checkStatus &&
        this.$refs.spec.score.check.coe < 0
      ) {
        this.$message.error("检查积分不能小于0");
        i += 1;
        return i;
      } else if (
        this.$refs.spec.checkStatus &&
        !regdec.test(this.$refs.spec.score.check.coe)
      ) {
        this.$message.error("检查积分为最多4位小数的数字");
        i += 1;
        return i;
      } else if (
        this.$refs.spec.textareaStatus == 1 &&
        this.$refs.spec.textareaValue == ""
      ) {
        this.$message.error("请输入超限原因");
        i += 1;
        return i;
      } else if (this.$refs.spec.totalWeight != 1) {
        console.log("Test", this.$refs.spec.totalWeight);
        this.$message.error("权重总和必须为1");
        i += 1;
        return i;
      } else if (this.$refs.spec.childLabel.label.length == 0) {
        this.$message.error("属性不能为空！");
        i += 1;
        return i;
      }
      return i;
    }
  },
  mounted: function() {
    var self = this;
    this.ue = window.UE.getEditor("editor", {
      autoHeightEnabled: false, //设置不自动调整高度
      elementPathEnabled: false,
      wordCount: false
    });
    //判断是否是流转任务,如果是，需要传上一个任务的batchid和taskid
    this.info.public.pickBatchId = this.GetQueryString("batchId");
    this.info.public.pickTaskId = this.GetQueryString("pickTaskId");
    this.info.public.taskId = this.GetQueryString("taskId");
    this.statusType = this.GetQueryString("status");
    this.batchId = this.GetQueryString("batchId");
    this.flowtask = this.GetQueryString("flowtask");
    this.groupId = this.GetQueryString("groupId");
    this.repulse = this.GetQueryString("repulse");
    
    this.Utils.getValue("batchId", function(id) {
      if (id != "") {
        id = id.substring(0, id.indexOf("&"));
        let proxyurl = "/v1/bes/task/batch?batchId=" + id;

        self.$api
          .method(proxyurl, "", "GET")
          .then(data => {
            if (data.rt.status == 200) {
              let _data = data.data[0];
              self.info.public.edit = true;
              self.info.public.batchId = _data.batchId;
              self.batchId = _data.batchId;
              self.info.public.taskName = _data.taskName;
              self.info.public.taskId = _data.taskId;
              self.info.public.batchName = _data.batchName;
              self.info.public.rule = _data.markStandard;
              self.info.public.level = _data.priority;
              if (_data.paused == null || _data.paused == undefined) {
                self.info.public.paused = 0;
              } else {
                self.info.public.paused = _data.paused;
              }
              self.priority = _data.priority;
              if (_data.extend.custom.base.recycleTime) {
                delete _data.extend.custom.base.recycleTime;
              }
              self.$set(self, "editBaseData", _data.extend.custom.base);
              self.$set(self, "editSpecData", _data.extend.custom.spec);

              self.flag = true;
            } else {
              self.$message(data.data);
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    });
    if (self.info.public.taskId != null) {
      var proxyurl = "/v1/bes/task/be/task?taskid=" + self.info.public.taskId;

      self.$api
        .method(proxyurl, "", "GET")
        .then(data => {
          if (data.rt.status == 200) {
            self.selectTask(self.info.public.taskId, data.data.name);
          } else {
            self.$message(data.data);
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
    
    this.ue.addListener("ready", function() {
      if (self.info.public.rule) {
        self.ue.setContent(self.info.public.rule);
      }
    });
  }
};
</script>